//
//  TSCreateMsgViewController.m
//  chart
//
//  Created by xu on 15/7/29.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSCreateMsgViewController.h"
#import "TSCreateMsgTableView.h"
#import "TSMutipleCheckTableViewController.h"
#import "TSMsgModel.h"
#import "TSMsgContentItemCell.h"
#import "TSMsgDatePickerTextFieldCell.h"
#import "TSPickerViewTextFieldCell.h"
#import "TSMsgModel.h"


@implementation TSCreateMsgViewController{
    TSCreateMsgTableView *tableView;
    TSMsgModel *msgModel;
}

@synthesize msgId;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    msgModel = [[TSMsgModel alloc] init];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentViewController:) name:TSNotification_PresentViewController object:nil];
    
    self.title = msgId?@"修改消息":@"创建消息";
    
    WS(weakSelf);
    [self rightBarButtonWithTitle:@"发送" barNorImage:nil barHltImage:nil action:^{
        if (!weakSelf) {
            return;
        }
        
        [weakSelf createMsg];
    }];
    
    tableView = [[TSCreateMsgTableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:tableView];
    
    if (msgId) {
        msgModel.requestParams = @{TSDictionaryKeyDefine_Id:msgId};
        [msgModel fetchWithPath:kRequestUrl_SendMessage_outOne_messageinfo completionHandler:^(BOOL isSucceeded, NSError *error) {
            if (!weakSelf) {
                return;
            }
            
            __strong typeof (weakSelf)strongSelf = weakSelf;
            
            strongSelf->tableView.msgModel = strongSelf->msgModel;
            [strongSelf->tableView reloadData];
        } NoProgress:NO];
    }
    else{
        [self leftBarButtonWithTitle:nil barNorImage:[UIImage imageNamed:@"nav_dark_close"] barHltImage:nil action:^{
            if (!weakSelf) {
                return;
            }
            
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }];
    }
}

/* 创建并发送消息 */
- (void)createMsg
{
    if (msgId) {
        [self updateMsg];
    }
    else{
        [self insertMsg];
    }
}

- (void)updateMsg
{
    WS(weakSelf);
    NSString *minites_str = [self getContentByTitle:@"时限:"];
    msgModel.requestParams = @{TSDictionaryKeyDefine_Id:msgId, @"title":[self getContentByTitle:@"标题:"], @"messageData":[self getContentByTitle:@"内容:"], @"time":[self getContentByTitle:@"时间:"], @"place":[self getContentByTitle:@"地点:"], @"sender":[self getContentByTitle:@"发送人:"], @"timeLimit":[NSString stringWithFormat:@"%zd", minites_str.integerValue]};
    [msgModel fetchWithPath:kRequestUrl_SendMessage_update completionHandler:^(BOOL isSucceeded, NSError *error) {
        if (!weakSelf) {
            return;
        }
        
        if (isSucceeded) {
            [[LBToastManager manager] showtoast:@"修改成功!"];
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:TSNotification_MessageChangeSucceeded object:nil];
        }
        else{
            [[LBToastManager manager] showtoast:@"修改失败!"];
        }
        
    } NoProgress:NO];
}

- (void)insertMsg
{
    WS(weakSelf);
    NSString *minites_str = [self getContentByTitle:@"时限:"];
    msgModel.requestParams = @{TSDictionaryKeyDefine_Title:[self getContentByTitle:@"标题:"], TSDictionaryKeyDefine_MessageData:[self getContentByTitle:@"内容:"], TSDictionaryKeyDefine_Time:[self getContentByTitle:@"时间:"], TSDictionaryKeyDefine_Place:[self getContentByTitle:@"地点:"], TSDictionaryKeyDefine_Sender:[self getContentByTitle:@"发送人:"], TSDictionaryKeyDefine_Recipient:[self getContentByTitle:@"接收人:"], TSDictionaryKeyDefine_TimeLimit:[NSString stringWithFormat:@"%zd", minites_str.integerValue]};
    [msgModel fetchWithPath:kRequestUrl_SendMessage_Insert completionHandler:^(BOOL isSucceeded, NSError *error) {
        if (!weakSelf) {
            return;
        }
        
        if (isSucceeded) {
            [[LBToastManager manager] showtoast:@"发布成功!"];
            
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:TSNotification_MessagePostSucceeded object:nil];
        }
        else{
            [[LBToastManager manager] showtoast:@"发布失败!"];
        }
        
    } NoProgress:NO];
}

- (NSString *)getContentByTitle:(NSString *)title
{
//    @[@"标题:", @"时间:", @"地点:", @"内容:", @"发送人:", @"接收人:", @"附件:", @"时限:"];

    NSString *content = @"";
    UITableViewCell *cell = [tableView getCellByTitle:title];
    
    if ([cell isKindOfClass:[TSMsgContentItemCell class]]) {
        TSMsgContentItemCell *item_cell = (TSMsgContentItemCell *)cell;
        content = item_cell.contentTextField.text;
    }
    else if ([cell isKindOfClass:[TSMsgDatePickerTextFieldCell class]]){
        TSMsgDatePickerTextFieldCell *date_cell = (TSMsgDatePickerTextFieldCell *)cell;

        content = [NSString stringWithFormat:@"%f", date_cell.contentTextField.datePicker.date.timeIntervalSince1970];
    }
    else if ([cell isKindOfClass:[TSPickerViewTextFieldCell class]]){
        TSPickerViewTextFieldCell *picker_cell = (TSPickerViewTextFieldCell *)cell;
        content = picker_cell.contentTextField.text;
    }
    
    return content;
}

- (void)presentViewController:(NSNotification *)noti
{
    TSMutipleCheckTableViewController *multiple_vc = [[TSMutipleCheckTableViewController alloc] init];
    TSBaseNavigationController *nav = [[TSBaseNavigationController alloc] initWithRootViewController:multiple_vc];
    [self presentViewController:nav animated:YES completion:nil];
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
